<template>
	<view>
		<u-navbar title="修改登录密码" :autoBack="true"></u-navbar>
		<view class="container">
			<view class="pass">
				<input type="password" v-model="opassword" placeholder="请输入旧密码"
					placeholder-style="color:#bdbdbd;font-size:13px" style="font-size: 28rpx;" />
				<!-- <image style="width: 40rpx;height: 28rpx;"
					src="https://img2.baidu.com/it/u=1229947238,3197261640&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
					mode="" /> -->
			</view>
			<view class="pass">
				<input password="password" v-model="password" placeholder="请输入新密码"
					placeholder-style="color:#bdbdbd;font-size:13px" style="font-size: 28rpx;" />
				<!-- <image style="width: 40rpx;height: 28rpx;"
					src="https://img2.baidu.com/it/u=1229947238,3197261640&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
					mode="" /> -->
			</view>
			<view class="pass">
				<input password="password" v-model="repassword" placeholder="请再次输入新密码"
					placeholder-style="color:#bdbdbd;font-size:13px" style="font-size: 28rpx;" />
				<!-- <image style="width: 40rpx;height: 28rpx;"
					src="https://img2.baidu.com/it/u=1229947238,3197261640&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
					mode="" /> -->
			</view>
			<view class="sure" @tap="newCode">确认</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				opassword: '',
				password: '',
				repassword: ''
			};
		},
		onLoad() {
			if (!this.hasLogin) {
				this.$api.msg('尚未登录，请先登录');
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}, 1000)
				return false;
			}
		},

		computed: {
			...mapState(['hasLogin']),
		},

		methods: {
			...mapMutations(['logout']),
			async newCode() {
				if (!this.opassword) {
					this.$api.msg('务必输入旧密码！')
					return
				}

				if (!this.password) {
					this.$api.msg('务必输入新密码！')
					return
				}

				if (this.password.length < 6) {
					this.$api.msg('登录密码最少6位数！');
					return false;
				}

				if (!this.repassword) {
					this.$api.msg('务必输入确认密码！')
					return
				}

				if (this.opassword == this.password) {
					this.$api.msg('旧密码和新密码不能一样！')
					return
				}

				if (this.password != this.repassword) {
					this.$api.msg('新密码和确认密码不一致！')
					return
				}

				let dataObj = {
					token: uni.getStorageSync('token'),
					opassword: this.opassword,
					password: this.password
				};

				let res = await this.$myRequest({
					url: '/changePwd1',
					data: dataObj,
					methods: 'post'
				})

				this.$api.msg(res.data.msg);
				uni.setStorageSync('token', '')
				this.logout();
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}, 1500)
			},

		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f2f2f2;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.container {
		position: absolute;
		top: 160rpx;
		width: calc(100% - 40rpx);
	}

	.pass {
		margin-top: 20rpx;
		padding: 20rpx 30rpx;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		border-radius: 10rpx;
	}

	.sure {
		margin-top: 50rpx;
		width: 100%;
		text-align: center;
		background-color: #fe6601;
		border-radius: 10rpx;
		font-size: 28rpx;
		color: #ffffff;
		padding: 20rpx 0;
	}
</style>